//  图片元信息保存类
//  ImageInfo.swift
//  YoruNeko
//
//  Created by liphone on 2021/7/2.
//

import Foundation

class ImageInfo:Hashable{
    
    /*
     相等只匹配url
     */
    static func == (lhs: ImageInfo, rhs: ImageInfo) -> Bool {
        lhs.url.hashValue == rhs.url.hashValue
    }
    

     public private(set) var url: URL
     public private(set) var width: Int?
     public private(set) var height: Int?
     
     var index:Int = -1;
    
     init(url: URL, width: Int?, height: Int?) {
         self.url = url
         self.width = width
         self.height = height
     }
     
    func hash(into hasher: inout Hasher) {
        url.hash(into: &hasher)
    }
    /*
     可失败构造器，如果传入url字符串无法生成的话
     */
    init?(urlStr:String,width:Int?,height:Int?){
        if let url = URL(string: urlStr){
            self.url = url
        }
        else{
            return nil
        }
        self.width = width
        self.height = height
    }
    
     //获取面积
     public var area: Int? {
         get {
             if let width = self.width {
                 if let height = self.height {
                     return width * height;
                 }
             }
             
             return nil
         }
     }
 }
